<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-row :gutter="20">
                <el-col :span="10">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select"
                        @keydown.native.enter="searchGood" @change="searchGood" clearable @clear="searchGood">
                        <el-button slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="14">
                     <el-button type="primary" @click="goAddpage">添加商品</el-button>
                </el-col>
            </el-row>

            <el-table border :data="goodsList">
                <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="商品价格" prop="goods_price" width="80px" align="center"></el-table-column>
                <el-table-column label="商品数量" prop="goods_number" width="80px" align="center"></el-table-column>
                <el-table-column label="创建时间" width="180px" align="center">
                    <template slot-scope="{row}">
                        {{row.add_time | dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180px">
                    <template slot-scope="{row,$index}">
                        <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGood(row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination @size-change="handleSizeChange" @current-change="getAllGoodsList"
                :current-page="queryInfo.pagenum" :page-sizes="[10, 15, 20]" :page-size="queryInfo.pagesize"
                layout=" prev, pager, next, jumper,->,sizes,total" :total="total">
            </el-pagination>
        </el-card>

    </div>
</template>

<script>
export default {
  name: 'GoodsList',

  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 3
      },
      total: 0,

      goodsList: []
    }
  },

  created () {
    this.getAllGoodsList()
  },

  methods: {
    async getAllGoodsList (paper = 1) {
      this.queryInfo.pagenum = paper
      const { data, meta } = await this.$http.goodsList.getAllGoodsList(this.queryInfo)
      console.log('data,meta: ', data, meta)
      if (meta.status === 200) {
        this.goodsList = data.goods
        this.total = data.total
      }
    },

    handleSizeChange (pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getAllGoodsList()
    },

    searchGood () {
      this.getAllGoodsList()
    },

    deleteGood (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data, meta } = await this.$http.goodsList.deleteGoodById(row.goods_id)
        if (meta.status === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })

          this.getAllGoodsList(this.goodsList.length > 1 ? this.queryInfo.pagenum : this.queryInfo.pagenum - 1)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 页面跳转 添加商品
    goAddpage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
    .el-table {
        margin: 20px 0;
    }
    .el-pagination  {
        text-align: center;
    }
</style>
